package com.mew.slots.core.common.quarze;

import com.mew.common.dc.redis.GameRoomRedisService;
import com.mew.common.entity.config.Game;
import com.mew.common.entity.config.Room;
import com.mew.common.entity.game.Jackport;
import com.mew.slots.core.common.handler.SlotsHandlerMapCmdType;
import com.mew.slots.core.common.handler.SlotsSessionManager;
import com.mew.slots.core.common.messge.response.PushJackPortMessage;
import com.mew.slots.core.common.utils.SlotsPushMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author dylin
 * @create 2021/5/17
 */
@Slf4j
@Component
public class PushJackPort {
    @Autowired
    private GameRoomRedisService gameRoomRedisService;
    @Autowired
    private SlotsSessionManager slotsSessionManager;

    @Scheduled(cron = "0/6 * * * * *")//6s一次
    public void push(){
        List<Game> gameList = gameRoomRedisService.findAllGame();
        if(null==gameList||gameList.isEmpty()){
            return;
        }
        for(Game game:gameList){
            List<Room> roomList=gameRoomRedisService.findRoomByGameId(game.getId());
            if(null==roomList||roomList.isEmpty()){
                continue;
            }
            for(Room room:roomList){
                List<Jackport> jList =gameRoomRedisService.findJackPortByRoomId(room.getId());
                if(null==jList||jList.isEmpty()){
                    continue;
                }
                SlotsPushMessageUtil.broadcastMessage(slotsSessionManager.findByRoom(room.getId()),
                        SlotsHandlerMapCmdType.JackPort.cmd(),
                        PushJackPortMessage.builder().jackPorts(jList).build());
            }
        }
    }
}
